//改变了用于判断是否已审核
var auditStateTemp = "";

var tableData = {
	url : ctp + "/pieceApplyController/getPieceApplyWorker.shtml?applyId=" + recId,
	hrefs : [[ "function=judgeReject", "workerId=WORKER_ID", "驳回", "[REJECT_STATE][已驳回=已驳回]" ]],
	propertys : [ "WORKER_NAME","JOBNO","GROUP_NAME","PROJECT_NAME","BEGIN_DATE","END_DATE","PIECE_NUM"],
	checkBox:["REC_ID",false],
	opers : true,
	trBgColor : ["tr-bgcolor", "REJECT_STATE=已驳回"],
	startLoad : function() {
		getListData(this);
	}
};
function searchData(curr) {
	setTagAndValue("form1", "curr", curr);
	setTagAndValue("form1","SUNNADA_PAGE_SIZE",5); 
	tableData.startLoad();
}

$(document).ready(function() {
	getPieceApply();
	searchData();
	getProcessCondition();
});

//获取申请单基本信息
function getPieceApply(){
	$.ajax({ 
	 	url: ctp + "/pieceApplyController/getPieceApplyByRecid.shtml",
	 	data: {"recId": recId, "auditId": auditId},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				var data = list[0];
				$("#applyTime").val(data.APPLY_TIME_CHAR);
				$("#applyPerson").val(data.NAME);
				$("#groupName").val(data.GROUP_NAME);
				if(data.AUDIT_STATE == '1' || data.AUDIT_STATE == '2'){
					auditStateTemp = data.AUDIT_STATE;
					$("#auditButton").hide();
					$("#auditDesc").val(data.AUDIT_DESC);
				}
			}
		}
	});
}

//获取当前流程审核情况
function getProcessCondition(){
	$.ajax({ 
	 	url: ctp + "/processController/getProcessConditionByrecid.shtml",
	 	data: {"recId": recId,"isView":"1","processType":"pieceApply"},
		type : 'POST',
		dataType: "json", 
		success : function(result) {
			if(result != null){
				var list = result.list;
				var judgeFlag = true;
				for(var i = 0; i < list.length; i++){
					var data = list[i];
					if(data.PARENT_ID == "00"){
						addProcessContion(data);
					}
					if(data.AUDIT_STATE == '待审核' || data.AUDIT_STATE == '审核不通过'){
						judgeFlag = false;
					}
				}
				var parentId = '0';
				iterateProcess(list, parentId);
				
				if(judgeFlag){
					completeHtml();
				}
			}
		}
	});
}

//循环遍历审批流程
function iterateProcess(list, parentId){
	for(var i = 0; i < list.length; i++){
		if(list[i].PARENT_ID == parentId){
			addProcessContion(list[i]);
			iterateProcess(list, list[i].AUDIT_ID);
		}
	}
}

function audit(value){
	var auditDesc = $("#auditDesc").val();
	if(auditDesc == ""){
		jalert("审核意见不能为空，请填写！");
		return;
	}
	$.ajax({ 
	 	url: ctp + "/pieceApplyController/audit.shtml",
	 	data: {"auditId": auditId,"auditState": value,"submitId": recId,"auditDesc": $("#auditDesc").val()},
		type : 'POST',
		success : function(result) {
			if(result == ''){
				jalert9("审核成功！");
				gotoBack();
			}else{
				jalert(result);
			}
		}
	});
}


function judgeReject(workerId){
	if(auditStateTemp == '1' || auditStateTemp == '2'){
		jalert("该申请单已审核，不能进行驳回操作！");
		return;
	}
	if($("#rowCount").html() == "1"){
		jalert("该申请单，申请内容只有1条不能进行驳回操作！");
		return;
	}
	jconfirm("reject('" + workerId + "')", "确定要驳回该记录？");
}

function reject(workerId){
	
	$.ajax({ 
	 	url: ctp + "/processController/reject.shtml",
	 	data: {"applyId": recId,"workerId": workerId,"rejectState": "2","processType":"pieceApply","auditId": auditId},
		type : 'POST',
		success : function(result) {
			if(result == ''){
				jalert9("驳回成功！");
				searchData();
			}else{
				jalert(result);
			}
		}
	});
}